var relationData;

var MethodData;

//sendData is the deliver json data
var sendData = {};
var deleteData = {};
var csvtemplate;
var wsdlpath;

function addEditDiv() {
	$("#treeEdit").append(
			"<h3>view tree and edit node</h3><br>" + "<label for='xPath'> path:</label> " + "<input type='text' id='xPath'>"
					+ "<label for='value'> value:</label> " + "<input type='text' id='value' ><br>" + "<input type='hidden' id='jstreeID'>"
					+ "<label for='dataElement'> dataElement:</label> " + "<div id='dataElement'></div><br>"
					+ "<input type='button' class='btn btn-default' id='add' value='add'>"
					+ "<input type='button' class='btn btn-default' id='delete' value='delete'>"
					+ "<input type='button' class='btn btn-default' id='deleteNode' value='delete this node'>"
					+ "<input type='button' class='btn btn-default' id='cancel' value='cancel'>"
					+ "<input type='button' class='btn btn-default' id='sendData' value='send data'>");
}

//从relation数据中获取"csv-key"项，并生成description的button
function addColumn(data) {
	dataSource = data;
	var keys = data["csv-key"].split('|');
	$.each(keys, function(k, v) {
		$("#dataElement").append("<button type='button' class='btn btn-default data' >" + v + "</button>");

	});
}

function getRelationData() {
	$.ajax({
		url : "../rest/relation/getRelationContent",
		data : {
			"path" : $("#path").val()
		},
		dataType : "JSON",
		method : "POST",
		success : function(data) {
			debugger
			relationData=data;
			wsdlpath=data.endpoint;
			csvtemplate=data.operationName;	
			getMethofdData(csvtemplate);
		},
		error : function(data) {
			// console.log();
		}
	});
}

function getMethofdData(operation) {
	debugger;
	$.ajax({
		type : "POST",
		url : "../rest/XMLNode",
		data : {
			"OperationName" : operation
		},
		dataType : "JSON",
		success : function(data) {
			debugger
			LoadingTree(data);
			editTreeNode();
		}
	});

}

function LoadingTree(data) {
	debugger
	$('#treeDiv').jstree({
		"core" : {
			"check_callback" : true,
			"data" : data,
		},
		"plugins" : [ "contextmenu" ]
	}).bind("select_node.jstree", function(event, data) {
		var xpath = data.node.original.path;
		var value = data.node.original.value;
		var id = data.node.id + "_anchor";
		debugger
		$("#treeEdit").html("");
		addEditDiv();
		$("#xPath").attr("value", xpath);
		$("#value").attr("value", value);
		$("#jstreeID").attr("value", id);
		addColumn(relationData);
		editButton();
		addJson();
		deleteJson();
		deleteNode();
		cancelToDeleteNode();
		sendJstreeData();
	})
	//    $("#treeDiv").jstree("open_all");
}

function editTreeNode() {
	$("#treeDiv a").click(function() {
		debugger
		$("#treeEdit").show();
	})
}

function editButton() {
	$("#dataElement button").click(function() {
		debugger
		$("#value").attr("value", $(this).text());
	})
}

function addJson() {
	$("#add").click(function() {
		debugger
		if ($("#xPath").val() != "" && $("#value").val() != "") {
			sendData[$("#xPath").val()] = $("#value").val();
			$("#" + $("#jstreeID").val()).attr("value", $("#value").val());
		}

	})
}

function deleteJson() {
	$("#delete").click(function() {
		if ($("#xPath").val() != "" && $("#value").val() != "")
			delete sendData[$("#xPath").val()];
	})

}

function deleteNode() {
	$("#deleteNode").click(function() {
		debugger
		$("#treeDiv #" + $("#jstreeID").val()).remove();
		//		$(this).remove("#treeDiv #"+$("#jstreeID").val());
		//		$("#treeDiv").jstree("remove","#j1_2_anchor");
		deleteData[$("#xPath").val()] = $("#xPath").val();
	})
}

function cancelToDeleteNode() {
	$("#cancel").click(function() {
		delete deleteData[$("#xPath").val()];
	})
}

function sendJstreeData() {
	$("#sendData").click(function() {
		debugger
		relationData["mappingData"]=sendData;
		relationData["deleteData"]=deleteData;
		relationData=[relationData];
		datasave = JSON.stringify(relationData);
		dataToJava = {
			"path" : $("#path").val(),
			"saveData" : datasave
		};
		$.ajax({
			url : "../rest/relation/editRelationFunction",
			dataType : "json",
			method : "POST",
			data : dataToJava,
			success : function(data) {
				debugger
				$("#messageArea").html("save success......");
			},
			error : function(data) {
				debugger
				$("#messageArea").html("connect error, status:" + data.status + " ,message:" + data.statusText);
			}
		});
	})
}

$(function() {
	getRelationData();
});